package com.edu.edu.week_project01.activity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.edu.edu.week_project01.R;
import com.edu.edu.week_project01.activity.Configs;
import com.edu.edu.week_project01.activity.bean.ProgrameBean;
import com.edu.edu.week_project01.activity.adapter.ProgrameAdapter;
import com.edu.edu.week_project01.activity.utils.OkHttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

public class ProgrameActivity extends AppCompatActivity {

    private final int LOAD_SUCCEDD = 0;
    private final int LOAD_FAILED = 1;
    private GridView mGv;

    public Handler mHandler = new Handler(){

        private List<ProgrameBean> list;

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case LOAD_SUCCEDD:
                    list = (List<ProgrameBean>) msg.obj;
                    adapter = new ProgrameAdapter(list);
                    mGv.setAdapter(adapter);
                    //点击子栏目跳转到该栏目直播页面
                    dropProgrameDetail();
                    break;
                case LOAD_FAILED:
                    Toast.makeText(ProgrameActivity.this,"网络请求失败",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    private ProgrameAdapter adapter;

    //点击子栏目跳转到该栏目直播页面
    private void dropProgrameDetail() {
        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                ProgrameBean bean = (ProgrameBean) adapter.getItem(position);
                //跳转到详情页面
                Intent intent = new Intent(ProgrameActivity.this,ProgrameDetailActivity.class);
                intent.putExtra("slug",bean);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_programe);
        //初始化控件
        mGv = (GridView) findViewById(R.id.programe_gv);
        //下载解析json
        LoadJson();
    }

    private void LoadJson() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = getSystemTime.getTime(Configs.JSON_URL_PROGRAME);
                String newUrl = OkHttpUtils.getJsonFromUrl(url);
                Gson gson=new Gson();
                List<ProgrameBean> data = gson.fromJson(newUrl,new TypeToken<List<ProgrameBean>>() {}.getType());
                Message meg = Message.obtain();
                if(data.size() > 0){
                    meg.what=LOAD_SUCCEDD;
                    meg.obj = data;
                }else {
                    meg.what=LOAD_FAILED;
                }
                mHandler.sendMessage(meg);
            }
        }).start();
    }

}
